package com.fm.base.json.impl;

import com.fm.base.json.JsonObject;
import com.fm.base.json.JsonValue;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author finalmachine
 */
public class BasicJsonObject extends LinkedHashMap<String, javax.json.JsonValue> implements JsonObject {
    @Override
    public JsonObject append(String key, boolean b) {
        return append(key, new BasicJsonBoolean(b));
    }

    @Override
    public JsonObject append(String key, int number) {
        put(key, number);
        return this;
    }

    @Override
    public JsonObject append(String key, long number) {
        put(key, number);
        return this;
    }

    @Override
    public JsonObject append(String key, double number) {
        put(key, number);
        return this;
    }

    @Override
    public JsonObject append(String key, Number number) {
        if (number instanceof BigDecimal) {
            return append(key, new BasicJsonNumber((BigDecimal) number));
        } else {
            return append(key, new BasicJsonNumber(number.toString()));
        }
    }

    @Override
    public JsonObject append(String key, String string) {
        return append(key, new BasicJsonString(string));
    }

    @Override
    public void put(String key, boolean b) {
        put(key, new BasicJsonBoolean(b));
    }

    @Override
    public void put(String key, int number) {
        put(key, new BasicJsonNumber(number));
    }

    @Override
    public void put(String key, long number) {
        put(key, new BasicJsonNumber(number));
    }

    @Override
    public void put(String key, double number) {
        put(key, new BasicJsonNumber(number));
    }

    @Override
    public void put(String key, Number number) {
        if (number instanceof BigDecimal) {
            put(key, new BasicJsonNumber((BigDecimal) number));
        } else {
            put(key, new BasicJsonNumber(number.toString()));
        }
    }

    @Override
    public void put(String key, String string) {
        put(key, new BasicJsonString(string));
    }

    @Override
    public javax.json.JsonValue put(String key, javax.json.JsonValue value) {
        return (JsonValue) super.put(key, (JsonValue) value);
    }

    @Override
    public ValueType getValueType() {
        return ValueType.OBJECT;
    }

    @Override
    public BasicJsonObject clone() {
        BasicJsonObject object = new BasicJsonObject();
        for (Map.Entry<String, javax.json.JsonValue> entry : entrySet()) {
            JsonValue value = (JsonValue) entry.getValue();
            object.put(entry.getKey(), value == null ? null : value.clone());
        }
        return object;
    }
}
